package com.leetcode.sort.basic_sort;

/**
 * @author Dennis Li
 * @date 2020/9/9 23:53
 */
public class InsertSort extends Sort {

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            int[] nums = generateArray(100000);
            sort(nums);
            System.out.println(isSorted(nums));
        }
        System.out.println(System.currentTimeMillis() - start);
    }

    public static void sort(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j > 0; j--) {
                if (nums[j] < nums[j - 1])
                    swap(nums, j, j - 1);
                else
                    break;
            }
        }
    }

    public static void sort2(int[] nums) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i; j > 0 && nums[j] < nums[j - 1]; j--) {
                swap(nums, j, j - 1);
            }
        }
    }

}
